package com.itools.core.service.impl;

import com.itools.core.annotation.FileStrategy;
import com.itools.core.base.CommonResult;
import com.itools.core.em.StrategyType;
import com.itools.core.log.Logable;
import com.itools.core.param.FmsUploadTokenParam;
import com.itools.core.result.FmsBusinessTypeResult;
import com.itools.core.result.FmsFileRecordPathResult;
import com.itools.core.result.FmsFileUploadResult;
import com.itools.core.result.FmsMultipartFileResult;
import com.itools.core.service.FileHandleService;
import com.itools.core.service.FileManagerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @project: itools-backend
 * @description:
 * @author: XUCHANG
 * @create: 2021-04-01 16:17
 */
@FileStrategy(value = StrategyType.NIO)
public class NioFileManagerServiceImpl implements FileManagerService {
    @Autowired
    private FileHandleService fileHandleService;
    /**
     * 批量上传文件
     * @param files 文件
     * @param uploadToken 文件的token
     * @param uploadTokenParam 文件的类型，以及限制条件
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public CommonResult<List<FmsMultipartFileResult>> mutipartUploadFiles(MultipartFile[] files,
                                                                          String uploadToken,
                                                                          FmsUploadTokenParam uploadTokenParam) {

        //执行上传文件的操作
        return fileHandleService.saveFileForNio(files,uploadToken,uploadTokenParam);
    }

    /**
     * 单个上传文件
     *
     * @param file  文件
     * @param uploadToken 文件的token
     * @param uploadTokenParam 文件的类型，以及限制条件
     * @param originalFilename 原名称
     * @return
     */
    @Override
    @Logable
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public CommonResult<FmsFileUploadResult> singletonUploadFile(MultipartFile file,
                                                                 String uploadToken,
                                                                 FmsUploadTokenParam uploadTokenParam,
                                                                 String originalFilename) {

        return fileHandleService.saveFileForNio(file, uploadTokenParam, uploadToken, originalFilename);
    }

    /**
     * 获取文件访问路径
     *
     * @param uniqueIds      List文件唯一id
     * @param expiredTime    过期时间
     * @param maxAccessCount 最大访问次数
     * @param type           文件下载 download/展示 show
     * @return
     */
    @Override
    public CommonResult<List<FmsFileRecordPathResult>> getFileUrlByUniqueIds(List<String> uniqueIds, Integer expiredTime, Integer maxAccessCount, String type) {
        return fileHandleService.getFileUrlByUniqueIds(uniqueIds,expiredTime,maxAccessCount,type);
    }

    /**
     * 下载文件
     *
     * @param fileAccessToken 文件的访问token
     * @param response
     * @param uniqueId        文件唯一id
     */
    @Override
    public void downloadFile(String fileAccessToken, HttpServletResponse response, String uniqueId) {
        fileHandleService.downloadFileNio(fileAccessToken,response,uniqueId);
    }
}
